<?php
App::uses('AppController', 'Controller');
/**
 * Customers Controller
 *
 * @property Customer $Customer
 * @property PaginatorComponent $Paginator
 */
class CustomersController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
	public $validate = array(
	        'name' => array(
	            '' => array(
	                'rule' => 'isUnique',
	                'required' => true,
	                'allowEmpty' => false,
	                'message' => 'Username nay da ton tai!!!'
	            )
	        ), 
	        'email' => array(
	            '' => array(
	                'rule' => 'isUnique',
	                'required' => true,
	                'allowEmpty' => false,
	                'message' => 'Email nay da ton tai!!!'
	            )
	        )
	    );
/**
 * index method
 *
 * @return void
 */
	 public function index() {
		$this->Customer->recursive = 0;
		$this->set('customers', $this->Paginator->paginate());
		$count = $this->Customer->Group->find('all');
	
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Customer->exists($id)) {
			throw new NotFoundException(__('Invalid customer'));
		}
		$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
		$this->set('customer', $this->Customer->find('first', $options));
		$this->theme='';
		$this->layout='';
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
	    	$email=$this->Customer->find( 'first', array('conditions' => (array('email'=>$this->data['email'])), 'recursive' => -1));
			if( $email!=null){
		        if($email!=null)
		        $this->set('existedemail','Email has used!</br>');
		    }else{
				$this->Customer->create();
				if ($this->Customer->save($this->request->data)) {
					$this->Session->setFlash(__('The customer has been saved.'));
					return $this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
				}
			}
		}
		$groups = $this->Customer->Group->find('list');
		$emails = $this->Customer->Email->find('list');
		$this->set(compact('groups', 'emails'));
	}
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Customer->exists($id)) {
			throw new NotFoundException(__('Invalid customer'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Customer->save($this->request->data)) {
				$this->Session->setFlash(__('The customer has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The customer could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
			$this->request->data = $this->Customer->find('first', $options);
		}
		$groups = $this->Customer->Group->find('list');
		$emails = $this->Customer->Email->find('list');
		$this->set(compact('groups', 'emails'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Customer->id = $id;
		if (!$this->Customer->exists()) {
			throw new NotFoundException(__('Invalid customer'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Customer->delete()) {
			$this->Session->setFlash(__('The customer has been deleted.'));
		} else {
			$this->Session->setFlash(__('The customer could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

	// public function list_customer_in_group($group_id, $email_id){
	public function list_customer_in_group(){
		if ($this->request->is('POST')) {
			$tempt_email = $_POST['form_data'];
			// pr($tempt_email);
			$group_id = $tempt_email['group'];
		$email_id = $tempt_email['email_id'];
			$this->theme = '';
			$this->layout= '';
		}
		
		// $this->set('tempt_email',$tempt_email);

		$this->loadmodel('Group');
		$options = array('conditions' => array('Customer.group_id' => $group_id),
			'recursive' => 1,
			'fields' => array('Customer.id', 'Customer.name', 'Customer.email')
			);
		$this->set('list_customer', $this->Customer->find('all', $options));
		$this->set('email_id', $email_id);
		$options = array('conditions' => array('Group.id' => $group_id),
			'recursive' => 1,
			'fields' => array( 'Group.name')
			);
		$this->set('group_name', $this->Group->find('list', $options));
	}
}
